import java.io.*;

public class InternetCheck {
    
    public boolean reach ;
    
    public InternetCheck() {
        /*try {
          InetAddress address = InetAddress.getByName("bbc.co.uk");
          System.out.println("Name: " + address.getHostName());
          System.out.println("Addr: " + address.getHostAddress());
          System.out.println("Reach: " + address.isReachable(3000));
        }
        catch (UnknownHostException e) {
          System.err.println("Unable to lookup web.mit.edu");
        }
        catch (IOException e) {
          System.err.println("Unable to reach web.mit.edu");
        }*/
        reach = getOS() ;
        System.out.println("reach = " + reach) ;
    }
    
    public static boolean ping( int mode) {
        boolean isReachable = false;
        try {
            Process proc ;
            if ( mode == 0 ) {
                proc = new ProcessBuilder("ping", "-n", "1", "last.fm").start();
            } else {
                proc = new ProcessBuilder("ping", "-c", "1", "-W", "4", "last.fm").start();
            }
            int exitValue = proc.waitFor();
            System.out.println("Exit Value:" + exitValue);
            if(exitValue == 0)
                isReachable = true;
        } catch (IOException e1) {
            System.out.println(e1.getMessage());
            e1.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return isReachable;
    }
    
    public static boolean getOS() {
        if (isWindows()) {
                System.out.println("This is Windows");
                return ping( 0 ) ;
        } else if (isMac()) {
                System.out.println("This is Mac");
                return false ;
        } else if (isUnix()) {
                System.out.println("This is Unix or Linux");
                return ping( 1 ) ;
        } else if (isSolaris()) {
                System.out.println("This is Solaris");
        } else {
                System.out.println("Your OS is not support!!");
        }
        return false ;
    }

    public static boolean isWindows() {

        String os = System.getProperty("os.name").toLowerCase();
        // windows
        return (os.indexOf("win") >= 0);

    }

    public static boolean isMac() {

        String os = System.getProperty("os.name").toLowerCase();
        // Mac
        return (os.indexOf("mac") >= 0);

    }

    public static boolean isUnix() {

        String os = System.getProperty("os.name").toLowerCase();
        // linux or unix
        return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);

    }

    public static boolean isSolaris() {

        String os = System.getProperty("os.name").toLowerCase();
        // Solaris
        return (os.indexOf("sunos") >= 0);

    }
}